<template>
	<view class="index">
		<view class="form">
			<u-form :model="form" ref="uForm" class="u-form">
				<u-form-item prop="username" left-icon="phone">
					<u-input placeholder="请输入手机号/邮箱" v-model="form.username"></u-input>
				</u-form-item>
				<u-form-item prop="password" left-icon="lock">
					<u-input type="password" placeholder="请输入密码" v-model="form.password"></u-input>
				</u-form-item>
			</u-form>
			<u-button 
				type="primary"
				class="submit-button"
				@click="submit"
			>登录</u-button>
		</view>
		<view class="message u-flex u-row-between">
			<text>通过手机验证码登录</text>
			<text>忘记密码？</text>
		</view>
		<u-toast ref="Toast"></u-toast>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				form: {
					username: '',
					password: ''
				},
				formRule: {
					username: [
						{
							required: true,
							message: '请输入用户名',
							trigger: ['change','blur']
						},
						{
							min:3,
							message: '用户名不能少于3位',
							trigger: ['change','blur']
						}
					],
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change','blur']
						},
						{
							min:3,
							message: '密码不能少于3为',
							trigger: ['change','blur']
						}
					]
				}
			};
		},
		methods: {
			...mapMutations(['saveUserinfo']),
			login(){
				return this.$api.user.login({
					...this.form
				})
				.then(res=>{
					if(res.code == 1){
						this.saveUserinfo(res.data)
						this.$refs.Toast.show({
							title: '登录成功',
							type: 'success',
							callback: ()=>{
								this.$Router.pushTab({name: 'user.index'})
							}
						})
					}else{
						this.$refs.Toast.show({
							title: res.msg,
							type: 'warning'
						})
					}
				})
			},
			submit(){
				this.$refs.uForm.validate(isValidate=>{
					if(isValidate)
						this.login()
				})
			}
		},
		mounted(){
			this.$refs.uForm.setRules(this.formRule)
		}
	}
</script>

<style lang="scss" scoped>
	.index{
		background-color: $uni-bg-color;
		.form{
			padding: 0 $uni-spacing-row-lg;
			.u-form{
				margin-bottom: 62upx;
			}
			.submit-button{
				width: 654upx;
				height: 90upx;
				border-radius: 90upx;
				color: #333333;
				font-weight: bold;
			}
		}
		.message{
			padding: 33upx $uni-spacing-row-lg;
			color: $uni-text-color-grey;
			font-size: 24upx;
		}
	}
</style>
